package lumis.portal.serviceinterface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;

import lumis.portal.*;
import lumis.portal.dao.*;
import lumis.portal.dao.hibernate.GenericHibernateManualUpdateDao;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.ITransaction;
import lumis.util.JdbcUtil;

/**
 * @version $Revision: 6669 $ $Date: 2007-05-02 15:52:18 -0300 (Wed, 02 May 2007) $
 * @since 4.0.0
 */
public class ServiceInterfaceDaoHib extends GenericHibernateManualUpdateDao<ServiceInterfaceConfig, String> implements IServiceInterfaceDao
{
	public ServiceInterfaceDaoHib()
	{
		super(ServiceInterfaceConfig.class);
	}
	
	protected String getIdentifier(ServiceInterfaceConfig serviceInterfaceConfig)
	{
		return serviceInterfaceConfig.getId();
	}
	
	public ServiceInterfaceConfig get(String serviceInterfaceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		return super.get(serviceInterfaceId, daoTransaction);
	}

	public void addOrUpdate(ServiceInterfaceConfig serviceInterfaceConfig, ITransaction daoTransaction) throws DaoException, PortalException
	{
		super.addOrUpdate(serviceInterfaceConfig, daoTransaction);
	}

	public boolean delete(ServiceInterfaceConfig serviceInterfaceConfig, ITransaction daoTransaction) throws DaoException, PortalException
	{
		return super.delete(serviceInterfaceConfig.getId(), daoTransaction);
	}

	public void setDefaultStyleId(String serviceInterfaceId, String serviceInterfaceStyleId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("update lum_Interface set defaultInterfaceStyleId=? where interfaceId=?");
			try
			{
				statement.setString(1, serviceInterfaceStyleId);
				statement.setString(2, serviceInterfaceId);				
				int rowsAffected = statement.executeUpdate();

				if (rowsAffected <= 0)
				{
					throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);
				}
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	public String getDefaultStyleId(String serviceInterfaceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select defaultInterfaceStyleId from lum_Interface where interfaceId=?");
			try
			{
				statement.setString(1, serviceInterfaceId);
				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
					{
						String defaultStyleId = resultSet.getString("defaultInterfaceStyleId");
						return defaultStyleId;
					}
					else
						throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);					
				}
				finally
				{
					resultSet.close();					
				}
			}
			finally
			{
				statement.close();				
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void setCustomDefaultStyleId(String serviceInterfaceId, String serviceInterfaceStyleId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("update lum_Interface set customDefaultInterfaceStyleId=? where interfaceId=?");
			try
			{
				statement.setString(1, serviceInterfaceStyleId);
				statement.setString(2, serviceInterfaceId);

				int rowsAffected = statement.executeUpdate();				
				if (rowsAffected <= 0)
				{
					throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);
				}
			}
			finally
			{
				statement.close();				
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public String getCustomDefaultStyleId(String serviceInterfaceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select customDefaultInterfaceStyleId from lum_Interface where interfaceId=?");
			try
			{
				statement.setString(1, serviceInterfaceId);
				ResultSet resultSet = statement.executeQuery();
				try
				{
					if (resultSet.next())
					{
						String defaultStyleId = resultSet.getString("customDefaultInterfaceStyleId");
						return defaultStyleId;
					}
					else
						throw new DaoException("STR_INTERFACE_DOES_NOT_EXIST;" + serviceInterfaceId);					
				}
				finally
				{
					resultSet.close();					
				}
			}
			finally
			{
				statement.close();				
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public Collection<String> getIdsByServiceId(String serviceId, ITransaction daoTransaction) throws DaoException, PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) daoTransaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			String strQuery = "select interfaceId from lum_Interface where serviceid = ?";

			PreparedStatement statement = connection.prepareStatement(strQuery);
			try
			{
				statement.setString(1, serviceId);
				return JdbcUtil.fillListString(statement, "interfaceId"); 				
			}
			finally
			{
				statement.close();				
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
}
